//
//  NetcodeHelper.m
//  BetsAndBrains
//
//  Created by Augustine, Eric M (augus011) on 4/28/12.
//  Copyright 2012 __MyCompanyName__. All rights reserved.
//

#import "NetcodeHelper.h"
#import <inttypes.h>

static NSString* SERVER_ADDRESS = @"banana.cs.uwp.edu";
static NSInteger SERVER_PORT = 54540;
// This goes in the first Byte of a message
static Byte BRAINS_AND_BETS = 0x2B;
// This goes in the second Byte of a message
static Byte MESSAGE_VERSION = 0x00;
// Packet Codes(Tied to the third Byte of a message)
static Byte CONNECTION_REQUEST = 0x00;
static Byte CONNECTION_RESPONSE = 0x01;
static Byte SERVER_LOBBY_INFO_REQUEST = 0x02;
static Byte SERVER_LOBBY_INFO_RESPONSE = 0x03;
static Byte JOIN_GAME_REQUEST = 0x04;
static Byte GAME_LOBBY_INFO = 0x05;
static Byte CREATE_GAME_REQUEST = 0x06;
static Byte START_GAME_REQUEST = 0x07;
static Byte QUESTION = 0x08;
static Byte ANSWER = 0x09;
static Byte BETTING_ODDS = 0x0A;
static Byte SUBMIT_BETS = 0x0B;
static Byte ROUND_RESULTS = 0x0C;
static Byte NEXT_ROUND_REQUEST = 0x0D;
static Byte LEAVE_GAME_REQUEST = 0x0E;
static Byte GAME_RESULTS = 0x0F;
static Byte DISCONNECT = 0x10;
static Byte ARE_YOU_THERE = 0x11;
static Byte ERROR = 0x12;
static Byte MESSAGE_RECEIVED = 0x13;
static Byte JOIN_GAME_INVITE = 0x14;

/*
 * Error Message Codes
 */
static Byte ERROR_BAD_GAME = 0x01;
static Byte ERROR_BAD_VER = 0x02;
static Byte ERROR_BAD_TYPE = 0x02;
static Byte ERROR_BAD_MESSAGE = 0x03;
static Byte ERROR_BAD_CLIENTID = 0x04;
static Byte ERROR_BAD_GAMEID = 0x05;
static Byte ERROR_FULL_GAME = 0x06;
static Byte ERROR_GAME_IN_PROGRESS = 0x07;
static Byte ERROR_GAME_NOT_FOUND = 0x08;
static Byte ERROR_END_OF_MESSAGE = 0x09;
//Why 0x7F for the last one? Because
//0XFF throws an error about it not being an NSInteger.
static Byte ERROR_OTHER = 0x7F;

static NSInteger DATA_OFFSET = 3;
static NSInteger BB_MESSAGE_ID_INDEX = 0;
static NSInteger BB_MESSAGE_VERSION_INDEX = 1;
static NSInteger BB_MESSAGE_TYPE_INDEX = 2;
static NSInteger MAX_MESSAGE_LENGTH = 8192;

static Byte END_OF_STRING = 0x00;
static Byte END_OF_RECORD = 0x03;
static Byte END_OF_MESSAGE = 0x04;

static NSInteger NUMBER_OF_SENDING_MESSAGE_ATTEMPTS = 10;
static NSInteger MILLISECONDS_BETWEEN_SENDING_MESSAGE = 100;

@implementation NetcodeHelper

+(NSInteger)byteArrayToInt:(NSArray*)byteArray{
	return ([[byteArray objectAtIndex:0] intValue] << 24) + (([[byteArray objectAtIndex:1] intValue] & 0xFF) << 16) + (([[byteArray objectAtIndex:2] intValue] & 0xFF) << 8) + ([[byteArray objectAtIndex:3] intValue] & 0xFF);
}

+(NSArray*)intToByteArray:(NSInteger)value{
	uint8_t b1 = value >> 24;
	uint8_t b2 = value >> 16;
	uint8_t b3 = value >> 8;
	uint8_t b4 = value;
	return [[NSArray alloc] initWithObjects:[[NSNumber alloc] initWithChar:b1],[[NSNumber alloc] initWithChar:b2],[[NSNumber alloc] initWithChar:b3],[[NSNumber alloc] initWithChar:b4], nil];
}

//This isnt likely to be correct
+(Byte*)generateMessageByteArray:(Byte)code{
	Byte *message = (Byte*)malloc(MAX_MESSAGE_LENGTH);
	message[BB_MESSAGE_ID_INDEX] = BRAINS_AND_BETS;
	message[BB_MESSAGE_VERSION_INDEX] = MESSAGE_VERSION;
	message[BB_MESSAGE_TYPE_INDEX] = code;
	return message;
}

+(NSString*)SERVER_ADDRESS{
	return SERVER_ADDRESS;
}

+(NSInteger)SERVER_PORT{
	return SERVER_PORT;
}

+(NSInteger)DATA_OFFSET{
	return DATA_OFFSET;
}

+(NSInteger)BB_MESSAGE_ID_INDEX{
	return BB_MESSAGE_ID_INDEX;
}

+(NSInteger)BB_MESSAGE_VERSION_INDEX{
	return BB_MESSAGE_VERSION_INDEX;
}

+(NSInteger)BB_MESSAGE_TYPE_INDEX{
	return BB_MESSAGE_TYPE_INDEX;
}

+(NSInteger)MAX_MESSAGE_LENGTH{
	return MAX_MESSAGE_LENGTH;
}

+(NSInteger)NUMBER_OF_SENDING_MESSAGE_ATTEMPTS{
	return NUMBER_OF_SENDING_MESSAGE_ATTEMPTS;
}

+(NSInteger)MILLISECONDS_BETWEEN_SENDING_MESSAGE{
	return MILLISECONDS_BETWEEN_SENDING_MESSAGE;
}

+(Byte)BRAINS_AND_BETS{
	return BRAINS_AND_BETS;
}

+(Byte)MESSAGE_VERSION{
	return MESSAGE_VERSION;
}

+(Byte)CONNECTION_REQUEST{
	return CONNECTION_REQUEST;
}

+(Byte)CONNECTION_RESPONSE{
	return CONNECTION_RESPONSE;
}

+(Byte)SERVER_LOBBY_INFO_REQUEST{
	return SERVER_LOBBY_INFO_REQUEST;
}

+(Byte)SERVER_LOBBY_INFO_RESPONSE{
	return SERVER_LOBBY_INFO_RESPONSE;
}

+(Byte)JOIN_GAME_REQUEST{
	return JOIN_GAME_REQUEST;
}

+(Byte)GAME_LOBBY_INFO{
	return GAME_LOBBY_INFO;
}

+(Byte)CREATE_GAME_REQUEST{
	return CREATE_GAME_REQUEST;
}

+(Byte)START_GAME_REQUEST{
	return START_GAME_REQUEST;
}

+(Byte)QUESTION{
	return QUESTION;
}

+(Byte)ANSWER{
	return ANSWER;
}

+(Byte)BETTING_ODDS{
	return BETTING_ODDS;
}

+(Byte)SUBMIT_BETS{
	return SUBMIT_BETS;
}

+(Byte)ROUND_RESULTS{
	return ROUND_RESULTS;
}

+(Byte)NEXT_ROUND_REQUEST{
	return NEXT_ROUND_REQUEST;
}

+(Byte)LEAVE_GAME_REQUEST{
	return LEAVE_GAME_REQUEST;
}

+(Byte)GAME_RESULTS{
	return GAME_RESULTS;
}

+(Byte)DISCONNECT{
	return DISCONNECT;
}

+(Byte)ARE_YOU_THERE{
	return ARE_YOU_THERE;
}

+(Byte)ERROR{
	return ERROR;
}

+(Byte)MESSAGE_RECEIVED{
	return MESSAGE_RECEIVED;
}

+(Byte)JOIN_GAME_INVITE{
	return JOIN_GAME_INVITE;
}

+(Byte)ERROR_BAD_GAME{
	return ERROR_BAD_GAME;
}

+(Byte)ERROR_BAD_VER{
	return ERROR_BAD_VER;
}

+(Byte)ERROR_BAD_TYPE{
	return ERROR_BAD_TYPE;
}

+(Byte)ERROR_BAD_MESSAGE{
	return ERROR_BAD_MESSAGE;
}

+(Byte)ERROR_BAD_CLIENTID{
	return ERROR_BAD_CLIENTID;
}

+(Byte)ERROR_BAD_GAMEID{
	return ERROR_BAD_GAMEID;
}

+(Byte)ERROR_FULL_GAME{
	return ERROR_FULL_GAME;
}

+(Byte)ERROR_GAME_IN_PROGRESS{
	return ERROR_GAME_IN_PROGRESS;
}

+(Byte)ERROR_GAME_NOT_FOUND{
	return ERROR_GAME_NOT_FOUND;
}

+(Byte)ERROR_END_OF_MESSAGE{
	return ERROR_END_OF_MESSAGE;
}

+(Byte)ERROR_OTHER{
	return ERROR_OTHER;
}

+(Byte)END_OF_STRING{
	return END_OF_STRING;
}

+(Byte)END_OF_RECORD{
	return END_OF_RECORD;
}

+(Byte)END_OF_MESSAGE{
	return END_OF_MESSAGE;
}


@end
